package service;


import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mybatis.mapper.CourseinfoMapper;
import com.mybatis.po.Courseinfo;

import util.LocalDateTimeSerializer;

@Service
public class CourseService {
	@Autowired
	private CourseinfoMapper courseinfomapper;
	
	public int addcourse(Courseinfo c1) {
		courseinfomapper.addCourse(c1);
		int cid=c1.getCid();
		return cid;
	}
	public int deletecourse(String c1) {
		return courseinfomapper.deleteCourse(c1);
	}
	public int updatecourse(Courseinfo c1) {
		return courseinfomapper.updateCourse(c1);
	}
	public List<Map<String,Object>> selectAllCourse(){
		List<Map<String,Object>> tmp =courseinfomapper.selectAllCourse();
//		System.out.println(tmp.get(0).get("tname"));
//		System.out.println(tmp.get(0).get("starttime"));
//		System.out.println(tmp.get(0).get("starttime").getClass());
		for(int i=0;i<tmp.size();i++) {
			tmp.get(i).replace("starttime", LocalDateTimeSerializer.serialize((LocalDateTime)tmp.get(i).get("starttime")));
		}
//		System.out.println(tmp.get(0).get("starttime"));
//		System.out.println(tmp.get(0).get("starttime").getClass());
		return tmp;
	}
	public String addvedio(List<MultipartFile> veido,String cid) {
		String tmpvedio="";
		//String path="D:/java/eclipse-workspace/sct/src/main/webapp/assets/img";
		//String path="";
		String path = CheckService.class.getResource("").getPath()+"../../../upload";
	      File f=new File(path);
	      if(!f.exists()){
	          f.mkdirs();//创建存储路径
	      }
		for(int i=0;i<veido.size();i++) {
			MultipartFile file=veido.get(i);
			String fileName=file.getOriginalFilename();
			//String fileName=session.getAttribute("Uid").toString()+".jpg";
			File targetFile=new File(path,fileName);
			tmpvedio=path+"/"+fileName;
			try {
				file.transferTo(targetFile);
				courseinfomapper.addplaybackaddress(cid, tmpvedio.substring(1));
			}catch(Exception e) {
				e.printStackTrace();
			}
	      }
		return tmpvedio.substring(1);
	}
}
